package com.xyazm.inventory.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.xyazm.inventory.domain.param.ReleaseProductStockParam;
import com.xyazm.inventory.service.IInventoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 监听 取消订单释放商品库存 消息 ok
 * @Author: xuyou
 * @Date: 2022/7/1 11:19
 */
@Slf4j
@Component
public class ReleaseInventoryListener implements MessageListenerConcurrently {

    @Autowired
    private IInventoryService inventoryService;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        try {
            for (MessageExt messageExt : list) {
                String message = new String(messageExt.getBody());
                log.info("ReleaseInventoryListener message:{}", message);
                ReleaseProductStockParam releaseProductStockParam = JSONObject.parseObject(message,
                        ReleaseProductStockParam.class);
                inventoryService.releaseProductStock(releaseProductStockParam);
                log.info("释放商品库存处理成功");
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        } catch (Exception e) {
            log.error("ReleaseInventoryListener error", e);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }


    }
}
